import Vue from 'vue'
import router from './router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import {
  ACCESS_TOKEN
} from '@/store/mutation-types'
NProgress.configure({
  showSpinner: false
}) // NProgress Configuration
let token = ''
router.beforeEach((to, from, next) => {
  NProgress.start() // start progress bar
  if (to.query.token) { // 有token获取接口
    token = to.query.token
    const originAppId = to.query.originAppId
    window.$postType(window.$api.upms.upms.loginByToken, { token, originAppId }).then(res => {
      Vue.ls.set(ACCESS_TOKEN, res.data)
      router.push({ query: {} })
      next()
    })
  } else { // 没token判断是否hash模式
    if (window.location.hash) {
      if (window.location.search.includes('token')) {
        const href = window.location.origin + '/' + window.location.hash + (window.location.hash.includes('?') ? decodeURIComponent(window.location.search).replace('?', '&') : window.location.search)
        window.location.href = href
      } else {
        next()
      }
    } else {
      next()
    }
  }
})
